package com.itzmn.newcode.offer1;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @Author: 张梦楠
 * @Date: 2018/12/20 15:24
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description: 从上向下打印二叉树，
 *
 * 从上往下打印出二叉树的每个节点，同层节点从左至右打印。
 *
 * 层序遍历
 */
public class _22_PrintBinaryTreeFromTopToBottom  extends Tree{

    public static void main(String[] args) {
        new _22_PrintBinaryTreeFromTopToBottom().test();
    }

    private void test() {

        TreeNode binaryTreeWithOnlyOneNull = createBinaryTreeWithOnlyOneNull(new int[]{1, 2, 3, 4, 5, 6});
        SequenceTraversal(binaryTreeWithOnlyOneNull);


    }

    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> integers = new ArrayList<>();
        if (root == null){
            return integers;
        }
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        treeNodes.add(root);
        while (!treeNodes.isEmpty()){
            TreeNode treeNode = treeNodes.removeFirst();
            System.out.print(treeNode.val+" ");
            integers.add(treeNode.val);
            if (treeNode.left != null){
                treeNodes.add(treeNode.left);
            }
            if (treeNode.right != null){
                treeNodes.add(treeNode.right);
            }
        }
        System.out.println();
        return integers;
    }
}
